<?php
namespace app\handler;

use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

class Qiniu
{
    private $accessKey = 'L2OvxAoGxcy2js2YfeQ76xrVKw6cFRudPEnsu27_';
    private $secretKey = '9IlKFsPQ4hA_R1OG3TltwlKmuU7W0J2D6B8LQuTZ';
    private $bucket = 'databm';

    public function upload($filePath,$key)
    {
        // 构建鉴权对象
        $auth = new Auth($this->accessKey, $this->secretKey);

        // 生成上传 Token
        $token = $auth->uploadToken($this->bucket);

        // 初始化 UploadManager 对象并进行文件的上传。
        $uploadMgr = new UploadManager();

        // 调用 UploadManager 的 putFile 方法进行文件的上传。
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);

        if ($err !== null) {
            return ['status'=>false,'msg'=>$err];
        } else {
            return ['status'=>true,'msg'=>$ret];
        }
    }
}